using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web.Mvc;
using Web.Model;
using System.IO;
using System.Linq;

namespace Web.HelperClasses.AvatarService
{
	public abstract class AvatarService
	{
        [HttpPost]
        public abstract void UploadNew(int Id, Stream avatarStream);

		public void RemoveAvatar(Avatar avatar)
		{
            foreach (Photo photo in avatar.AllPhotos.Where(p => p!= null))
            {
                yandexFotki.Delete(photo);
            }
		}

		public AvatarService(YandexFotki yandexFotki)
        {
			this.yandexFotki = yandexFotki;
        }

		protected static Bitmap ResizeImage(Image imgToResize, int width)
		{
			int sourceWidth = imgToResize.Width;
			int sourceHeight = imgToResize.Height;

			var nPercent = (float)width / sourceWidth;
			int destHeight = (int)(sourceHeight * nPercent);

			var b = new Bitmap(width, destHeight);
			using (var g = Graphics.FromImage(b))
			{
				g.InterpolationMode = InterpolationMode.HighQualityBicubic;
				g.DrawImage(imgToResize, 0, 0, width, destHeight);
			}

			return b;
		}

		protected readonly YandexFotki yandexFotki;		
	}
}